package cn.afactory.tygz.ui.base.utils;

import android.app.Activity;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.BackgroundColorSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import cn.afactory.tygz.R;
import cn.afactory.tygz.common.RoleType;
import cn.afactory.tygz.model.user.UserTag;
import cn.afactory.tygz.ui.base.utils.sns.Tools;

/**
 * Created by Kenneth on 15/9/9.
 */
public class ViewUtils {
    public static <T> T findView(Activity activity, int resId) {
        T v = null;
        try {
            v = (T) activity.findViewById(resId);
        } catch (Exception e) {
            throw new NullPointerException("Not find the view by id:" + resId);
        }
        return v;
    }

    public static <T> T findView(View view, int resId) {

        T v = null;
        try {
            v = (T) view.findViewById(resId);
        } catch (Exception e) {
            throw new NullPointerException("Not find the view by id:" + resId);
        }
        return v;
    }

    /**
     * 绑定标签View
     * @param type
     * @param v
     */
    public static void applyTagView(int type, ImageView v, LinearLayout linearLayout){
        if(type == RoleType.GOLDEN){
            v.setVisibility(View.VISIBLE);
            v.setImageResource(R.drawable.v);
            linearLayout.setBackgroundResource(R.drawable.gold_v);
            linearLayout.setPadding(
                    Tools.dip2px(v.getContext(),3),
                    Tools.dip2px(v.getContext(),3),
                    Tools.dip2px(v.getContext(),3),
                    Tools.dip2px(v.getContext(),3));
        }else if(type == RoleType.SILVER){
            v.setVisibility(View.VISIBLE);
            v.setImageResource(R.drawable.v);
            linearLayout.setBackgroundResource(R.drawable.sil_v);
            linearLayout.setPadding(
                    Tools.dip2px(v.getContext(),3),
                    Tools.dip2px(v.getContext(),3),
                    Tools.dip2px(v.getContext(),3),
                    Tools.dip2px(v.getContext(),3));
        }else if(type == RoleType.NONE){
            v.setVisibility(View.GONE);
        }
    }

    /**
     * 绑定徽章的名称
     * @param tags
     * @param v
     */
    public static void applyTagName(List<UserTag> tags, TextView v){
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
        for(UserTag userTag :tags) {
            SpannableString spannableString = new SpannableString(userTag.name);
            spannableString.setSpan(new ForegroundColorSpan(Color.WHITE), 0, userTag.name.length(),
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//            spannableString.setSpan(new BackgroundColorSpan(Color.RED), 0, userTag.name.length(),
//                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            spannableStringBuilder.append(spannableString);
            spannableStringBuilder.append(" ");
        }
        v.setText(spannableStringBuilder);
    }

}
